<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>产品服务</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1000px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .products {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        .product-card {
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 15px;
            background-color: white;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            transition: transform 0.3s;
        }
        .product-card:hover {
            transform: translateY(-5px);
        }
        .product-name {
            font-size: 1.2em;
            font-weight: bold;
            margin-bottom: 10px;
            color: #333;
        }
        .product-description {
            color: #666;
            margin-bottom: 15px;
        }
        .product-price {
            font-weight: bold;
            color: #4CAF50;
            font-size: 1.2em;
            margin-bottom: 10px;
        }
        .product-stock {
            color: #2196F3;
        }
        .stock-low {
            color: #F44336;
        }
        .add-form {
            margin-top: 30px;
            background-color: #f9f9f9;
            padding: 20px;
            border-radius: 5px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>产品管理系统</h1>
        <div id="productsList" class="products">
            <!-- 产品数据将在这里动态加载 -->
        </div>

        <div class="add-form">
            <h2>添加新产品</h2>
            <form id="addProductForm">
                <div class="form-group">
                    <label for="name">产品名称</label>
                    <input type="text" id="name" name="name" required>
                </div>
                <div class="form-group">
                    <label for="description">产品描述</label>
                    <textarea id="description" name="description" rows="3" required></textarea>
                </div>
                <div class="form-group">
                    <label for="price">价格</label>
                    <input type="number" id="price" name="price" step="0.01" min="0" required>
                </div>
                <div class="form-group">
                    <label for="stock">库存数量</label>
                    <input type="number" id="stock" name="stock" min="0" required>
                </div>
                <button type="submit">添加产品</button>
            </form>
        </div>
    </div>

    <script>
        // 页面加载时获取产品数据
        window.onload = function() {
            fetchProducts();
            
            // 添加产品表单提交事件
            document.getElementById('addProductForm').addEventListener('submit', function(e) {
                e.preventDefault();
                addProduct();
            });
        };

        // 获取产品数据
        function fetchProducts() {
            fetch('/api/products')
                .then(response => response.json())
                .then(data => {
                    const productsContainer = document.getElementById('productsList');
                    productsContainer.innerHTML = '';
                    
                    data.forEach(product => {
                        const productCard = document.createElement('div');
                        productCard.className = 'product-card';
                        
                        // 设置库存状态样式类
                        const stockClass = product.stock < 10 ? 'stock-low' : '';
                        
                        productCard.innerHTML = `
                            <div class="product-name">${product.name}</div>
                            <div class="product-description">${product.description}</div>
                            <div class="product-price">¥${product.price}</div>
                            <div class="product-stock ${stockClass}">库存: ${product.stock} 件</div>
                        `;
                        
                        productsContainer.appendChild(productCard);
                    });
                })
                .catch(error => {
                    console.error('获取产品数据时出错:', error);
                });
        }

        // 添加新产品
        function addProduct() {
            const form = document.getElementById('addProductForm');
            const product = {
                name: form.name.value,
                description: form.description.value,
                price: parseFloat(form.price.value),
                stock: parseInt(form.stock.value)
            };
            
            fetch('/api/products', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(product)
            })
            .then(response => response.json())
            .then(data => {
                console.log('添加产品成功:', data);
                form.reset();
                fetchProducts(); // 重新加载产品列表
            })
            .catch(error => {
                console.error('添加产品时出错:', error);
            });
        }
    </script>
</body>
</html>